package cn.iocoder.yudao.module.alipay.controller.admin.infoproductiontype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.infoproductiontype.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.infoproductiontype.InfoProductionTypeDO;
import cn.iocoder.yudao.module.alipay.convert.infoproductiontype.InfoProductionTypeConvert;
import cn.iocoder.yudao.module.alipay.service.infoproductiontype.InfoProductionTypeService;

@Tag(name = "管理后台 - 作品分类")
@RestController
@RequestMapping("/alipay/info-production-type")
@Validated
public class InfoProductionTypeController {

    @Resource
    private InfoProductionTypeService infoProductionTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建作品分类")
    @PreAuthorize("@ss.hasPermission('alipay:info-production-type:create')")
    public CommonResult<Long> createInfoProductionType(@Valid @RequestBody InfoProductionTypeCreateReqVO createReqVO) {
        return success(infoProductionTypeService.createInfoProductionType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新作品分类")
    @PreAuthorize("@ss.hasPermission('alipay:info-production-type:update')")
    public CommonResult<Boolean> updateInfoProductionType(@Valid @RequestBody InfoProductionTypeUpdateReqVO updateReqVO) {
        infoProductionTypeService.updateInfoProductionType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除作品分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:info-production-type:delete')")
    public CommonResult<Boolean> deleteInfoProductionType(@RequestParam("id") Long id) {
        infoProductionTypeService.deleteInfoProductionType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得作品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:info-production-type:query')")
    public CommonResult<InfoProductionTypeRespVO> getInfoProductionType(@RequestParam("id") Long id) {
        InfoProductionTypeDO infoProductionType = infoProductionTypeService.getInfoProductionType(id);
        return success(InfoProductionTypeConvert.INSTANCE.convert(infoProductionType));
    }


    @GetMapping("/list")
    @Operation(summary = "获得作品分类列表")
    @PreAuthorize("@ss.hasPermission('alipay:info-production-type:query')")
    public CommonResult<List<InfoProductionTypeRespVO>> getInfoProductionTypeList() {
        List<InfoProductionTypeDO> list = infoProductionTypeService.getInfoProductionTypeList();
        return success(InfoProductionTypeConvert.INSTANCE.convertList(list));
    }


    @GetMapping("/page")
    @Operation(summary = "获得作品分类分页")
    @PreAuthorize("@ss.hasPermission('alipay:info-production-type:query')")
    public CommonResult<PageResult<InfoProductionTypeRespVO>> getInfoProductionTypePage(@Valid InfoProductionTypePageReqVO pageVO) {
        PageResult<InfoProductionTypeDO> pageResult = infoProductionTypeService.getInfoProductionTypePage(pageVO);
        return success(InfoProductionTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出作品分类 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:info-production-type:export')")
    @OperateLog(type = EXPORT)
    public void exportInfoProductionTypeExcel(@Valid InfoProductionTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InfoProductionTypeDO> list = infoProductionTypeService.getInfoProductionTypeList(exportReqVO);
        // 导出 Excel
        List<InfoProductionTypeExcelVO> datas = InfoProductionTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "作品分类.xls", "数据", InfoProductionTypeExcelVO.class, datas);
    }

}
